<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <style>
        /* 添加导航栏的样式 */
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
            display: flex;
            flex-direction: column;
        }

        /* 添加导航栏的样式 */
        nav {
            background-color: #468ef3; /* 导航栏背景颜色 */
            padding: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-radius: 10px;
            width: 90%;
            margin: 20px auto;
        }

        nav a {
            color: white; /* 链接的文字颜色 */
            text-decoration: none; /* 去掉下划线 */
            padding: 10px 20px;
        }

        nav a:hover {
            background-color: #669fee; /* 悬停时背景颜色变化 */
            border-radius: 5px;
        }

        #bar {
            width: 0%;
            height: 30px;
            background-color: #4caf50;
            text-align: center;
            line-height: 30px;
            color: white;
        }

        .elements {
            width: 90%;
            margin: 10px auto;
        }

        .upload-container {
            margin: 15px auto;
            display: flex;
            flex-wrap: wrap;
            background-color: #ffffffc3;
            border-radius: 5px;
            align-items: center; /* 垂直居中 */
        }

        #progress {
            width: 95%;
            background-color: #b1b1b1;
            margin: 10px auto;
            margin-bottom: 15px;
            border-radius: 5px;
        }

        #uploadBox {
            width: 100%;
            margin-top: 10px;
            margin-left: 15px;
        }

        .image-container {
            /* border: 2px solid black;
            padding: 5px;
            margin: 5px; */
            
            margin-bottom: 10px;
            width: 100%;
            display: flex;
            
        }
        .image-container img {
            max-width: 70%;
            max-height: 500px;
            margin-top: 15px;
            margin-left: 15px;
        }

        .content-container {
            display: flex;
            justify-content: space-between;
            width: 100%;
        }

        .countdown, .spring-countdown {
            width: 45%;
            margin: 0; /* 上下边距为25px，左右边距为0 */
            background-color: #ffffffc3;
            border-radius: 5px;
            display: inline-flex; /* 使其在父容器中表现为块元素，但仍可左对齐 */
            padding: 15px;
            font-size: larger;
            justify-content: center;
            align-items: flex-start;
            flex-direction: column;
            font-weight: bold;
            line-height: 2;
        }

        .image-discription {
            max-width: 70%;
            max-height: 500px;
            margin-left: 15px;
        }

        .daily-mottos {
            margin: 25px auto;
            display: flex;
            flex-wrap: wrap;
            flex-direction: column;
            background-color: #ffffffc3;
            border-radius: 5px;
            align-items: center; /* 垂直居中 */
            justify-content: center;
        }

        #motto-title {
            margin-top: 25px;
            align-items: center;
            text-align: center;
            font-size: 175%;
            font-family: '华文中宋';
            font-weight: bold;
            line-height: 2;
        }

        #motto-content {
            align-items: center;
            text-align: center;
            font-family: '华文中宋';
            font-size: 125%;
            margin-bottom: 25px;
            line-height: 2;
        }

        body {
            background-image: url("background.png");
            background-size: cover;
            background-position-x: center;
            background-repeat: no-repeat;
            height: 100vh;
            width: 100vw;
        }

        footer {
            background-color: #468ef3; /* 底栏背景颜色 */
            color: white; /* 底栏文字颜色 */
            text-align: center; /* 文本居中 */
            padding: 10px 0; /* 上下内边距 */
            margin-top: auto; /* 将底栏推到页面底部 */
        }

        .footer-text {
            font-size: smaller;
            line-height: 2;
            color: #bdd2f0;
        }

        .footer-text a {
            line-height: 2;
            color: #bdd2f0;
        }

        @media screen and (max-width: 1152px) {
            .elements {
                width: 90%;
                display: flex;
                flex-direction: column;
                align-items: center;
            }

            .container {
                width: 100%;
                margin: 25px auto;
                display: flex;
                flex-wrap: wrap;
                background-color: #ffffffc3;
                border-radius: 5px;
                align-items: center; /* 垂直居中 */
            }

            #progress {
                width: 95%;
                background-color: #b1b1b1;
                margin: 10px auto;
                margin-bottom: 15px;
                border-radius: 5px;
            }

            #uploadBox {
                width: 100%;
                margin-top: 10px;
                margin-left: 0;
            }

            .content-container {
                display: flex;
                justify-content: center;
                width: 100%;
                flex-direction: column;
            }

            .countdown, .spring-countdown {
                width: 95%;
                margin: 0; /* 上下边距为25px，左右边距为0 */
                background-color: #ffffffc3;
                border-radius: 5px;
                display: flex; /* 使其在父容器中表现为块元素，但仍可左对齐 */
                padding: 15px;
                flex-wrap: wrap;
                font-size: larger;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                font-weight: bold;
                line-height: 2;
                margin-top: 10px;
            }
        }
    </style>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
</head>

<body>
    <nav>
        <a href="/">首页</a>
        <a href="/c15">政治</a>
        <a href="/g">画廊</a>
    </nav>
    
    <div class="elements">
        <div class="upload-container">
            <div id="uploadBox">
                <form id="uploadForm" action="/upload" method="post" enctype="multipart/form-data">
                    <input type="file" id="file" name="file" accept="image/*, video/*" multiple required>
                    <button type="submit" id="uploadButton">上传</button>
                </form>
            </div>
                
            <div id="progress">
                <div id="bar"></div>
            </div>
        </div>
    
        <div class="content-container">
            <div class="countdown">
                <div id="countdown1">我们相遇已经 </div>
                <div id="countdown2">网盘公测已经 </div>
                <div id="countdown3">上次更新距今 </div>
            </div>
            
            <div class="spring-countdown">
                <div class="image-container"><img src="" alt="Spring Countdown Image"></div>
                <div class="image-discription">
                    <p id="todayDate">公历日期: </p>
                    <p id="lunarDate">农历日期: </p>
                    <p id="sijiuProgress">数九进度: </p>
                </div>
            </div>
        </div>

        <div class="daily-mottos">
            <div id="motto-title">每日金句</div>
            <div id="motto-content"></div>
        </div>
    </div>
    
    <div id="message"></div>

    <script>
        var form = document.getElementById('uploadForm');
        var fileInput = document.getElementById('file');
        var progressBar = document.getElementById('bar');
        var messageBox = document.getElementById('message');

        form.addEventListener('submit', function (e) {
            e.preventDefault();
            var formData = new FormData();

            // Append all selected files to FormData
            for (var i = 0; i < fileInput.files.length; i++) {
                formData.append('file', fileInput.files[i]);
            }

            var xhr = new XMLHttpRequest();

            xhr.upload.addEventListener('progress', function (event) {
                if (event.lengthComputable) {
                    var percentComplete = (event.loaded / event.total) * 100;
                    progressBar.style.width = percentComplete + '%';
                    progressBar.innerHTML = percentComplete.toFixed(2) + '%';
                }
            });

            xhr.onload = function () {
                messageBox.innerHTML = '上传完成！';
                progressBar.style.width = '0%';
                // Show alert when upload completes
                alert('上传完成！点击确定重新加载页面。');
                // Reload the page after user confirms
                window.location.reload();
            };

            xhr.onerror = function () {
                messageBox.innerHTML = '上传失败！';
            };

            xhr.open('POST', '/upload', true);
            xhr.send(formData);
        });

        // 定义三个过去的时间点
        var pastDates = [
            new Date('2024-08-20T16:00:00+08:00'), // 北京时间 2024-08-20 16:00
            new Date('2024-10-13T21:42:00+08:00'), // 北京时间 2024-10-13 21:42
            new Date('2025-01-19T19:00:00+08:00')  // 北京时间 2025-01-19 19:00
        ];

        function updateCountdown() {
            var now = new Date();

            pastDates.forEach(function(date, index) {
                var diff = Math.floor((now - date) / 1000); // 计算时间差（秒）

                var days = Math.floor(Math.abs(diff) / (3600 * 24));
                var hours = Math.floor((Math.abs(diff) % (3600 * 24)) / 3600);
                var minutes = Math.floor((Math.abs(diff) % 3600) / 60);
                var seconds = Math.abs(diff) % 60;

                // 相关内容
                var relatedText = '';
                switch (index) {
                    case 0:
                        relatedText = '我们相遇已经 ';
                        break;
                    case 1:
                        relatedText = '网盘公测已经 ';
                        break;
                    case 2:
                        relatedText = '上次更新距今 ';
                        break;
                }

                // 更新倒计时
                if (diff < 0) {
                    document.getElementById('countdown' + (index + 1)).innerHTML = 
                        '距离下次更新 ' + days + '天 ' + hours + '小时 ' + minutes + '分钟 ' + seconds + '秒';
                } else {
                    document.getElementById('countdown' + (index + 1)).innerHTML = 
                        relatedText + days + '天 ' + hours + '小时 ' + minutes + '分钟 ' + seconds + '秒';
                }
            });
        }

        // 每秒更新倒计时显示
        setInterval(updateCountdown, 1000);


        

        document.addEventListener('DOMContentLoaded', function() {
            fetch('/motto')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('motto-content').innerText = data.content;
                })
                .catch(error => console.error('Error fetching motto:', error));
        });

        // 获取 spring countdown 数据并填充到 div 中
        function updateSpringCountdown() {
            fetch('/spring-countdown-data')
                .then(response => response.json())
                .then(data => {
                    var imageContainer = document.querySelector('.spring-countdown .image-container img');
                    var imageDescription = document.querySelector('.spring-countdown .image-discription');
                    
                    if (imageContainer && imageDescription) {
                        imageContainer.src = data.image_url;
                        imageDescription.innerHTML = `
                            公历日期: ${data.gregorian_date}<br>
                            农历日期: ${data.lunar_date}<br>
                            ${data.sijiu_progress}
                        `;
                    } else {
                        console.error('Error: .spring-countdown elements not found');
                    }
                })
                .catch(error => console.error('Error fetching spring countdown data:', error));
        }



        // 页面加载时更新 spring countdown
        updateSpringCountdown();
        

        document.addEventListener('DOMContentLoaded', function() {
            fetch('/motto')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('motto-content').innerText = data.content;
                })
                .catch(error => console.error('Error fetching motto:', error));
        });
    </script>

    <footer>
        <img src="footer.png" alt="Footer Image" style="display: block; margin: 0 auto; max-width: 60%; max-height: 125px;"/>
        <div class="footer-text">
            Made with ❤️ by dev group:
            <br>
            <a href="https://space.bilibili.com/562001644?spm_id_from=..0.0">Steven_Lee_147</a>, 
            <a href="https://space.bilibili.com/1718299194?spm_id_from=..0.0">CharlesZiy</a>, 
            Fitten Code, ChatGPT, and Github Copilot.
            <br>
            友链：
            <a href="https://space.bilibili.com/693772585?spm_id_from=..0.0">某知名英语老师</a> |
            <a href="https://memoir.rdfz.app/">黄庄回响集</a>

        </div>
    </footer>
</body>
</html>
